package com.android.common.base.rv;

import android.text.Html;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import com.android.common.R;
import com.android.common.mvp.model.BaseMultiSelectBean;
import com.android.common.mvp.view.ToolBarBinding;
import java.util.HashMap;
import java.util.Map;

/**
 * 多选列表
 * @param <T>
 */
public abstract class BaseMultiSelectRvFragment<T extends BaseMultiSelectBean>
    extends BaseRvFragment<T> implements ToolBarBinding.ToolbarLeftListener {

    protected boolean mSelectMode = false;
    /**
     * Integer 数据下标
     * T Adapter中所放数据类型
     */
    protected Map<Integer,T> mSelectMap;
    protected BaseRvAdapter<T> mAdapter;
    protected View mBottomView;
    protected String mSelectCountDesc;
    protected TextView mTvSelected;
    protected CheckBox mRbSelectAll;

    @Override
    protected void onFindView() {
        super.onFindView();
        mSelectCountDesc = String.format(getString(R.string.have_selected),requireActivity().getColor(R.color.color_5178FF), 0);
    }

    @Override
    protected void onInitView() {
        super.onInitView();
        onInitMultiSelect();
    }

    @Override
    protected View onCreateRvBottomView() {
        mBottomView = mLayoutInflater.inflate(R.layout.view_select_rv_bottom,null);
        mRbSelectAll = mBottomView.findViewById(R.id.cb_select_all);
        mTvSelected = mBottomView.findViewById(R.id.tv_selected);
        mTvSelected.setText(Html.fromHtml(mSelectCountDesc,Html.FROM_HTML_MODE_LEGACY));
        mRbSelectAll.setOnCheckedChangeListener((compoundButton, checked) -> {
            if (compoundButton.isPressed()){
                if (checked){
                    mSelectMap.clear();
                    for (int index = 0; index < mAdapter.getData().size(); index++){
                        T bean = mAdapter.getData().get(index);
                        bean.setSelected(true);
                        mAdapter.updateItem(index);
                        mSelectMap.put(index,bean);
                    }
                    mSelectCountDesc = String.format(getString(R.string.have_selected), requireActivity().getColor(R.color.color_5178FF),mSelectMap.size());
                    mTvSelected.setText(Html.fromHtml(mSelectCountDesc,Html.FROM_HTML_MODE_LEGACY));
                }else {
                    mSelectMap.clear();
                    for (int index = 0; index < mAdapter.getData().size(); index++){
                        T bean = mAdapter.getData().get(index);
                        bean.setSelected(false);
                        mAdapter.updateItem(index);
                    }
                    mSelectCountDesc = String.format(getString(R.string.have_selected), requireActivity().getColor(R.color.color_5178FF),mSelectMap.size());
                    mTvSelected.setText(Html.fromHtml(mSelectCountDesc,Html.FROM_HTML_MODE_LEGACY));
                }
            }
        });

        mBottomView.setVisibility(View.GONE);
        return mBottomView;
    }

    protected void onInitMultiSelect(){
        mAdapter.setOnItemLongClickListener((view,position) ->{
            for (int index = 0; index < mAdapter.getData().size(); index++){
                T bean = mAdapter.getData().get(index);
                bean.setVisible(true);
                mAdapter.updateItem(index);
            }
            if (null != mBottomView){
                mBottomView.setVisibility(View.VISIBLE);
            }
            mToolBarBinding.showToolbarLeftTextView(true);
            mToolBarBinding.showBack(false);
            mSelectMode = true;
            setEnableRefresh(false);
            setEnableLoadMore(false);
            mSelectMap = new HashMap<>();
        });
    }

    protected void onRestoreViewStatus(){
        if (mBottomView != null){
            mBottomView.setVisibility(View.GONE);
        }
        mToolBarBinding.showBack(true && onNeedBack());
        mToolBarBinding.showToolbarLeftTextView(false);
        mSelectMode = false;
        setEnableRefresh(true);
        setEnableLoadMore(true);
        mSelectMap.clear();
        mSelectMap = null;
        for (int index = 0; index < mAdapter.getData().size(); index++){
            T bean = mAdapter.getData().get(index);
            bean.setVisible(false);
            bean.setSelected(false);
            mAdapter.updateItem(index);
        }
    }

    @Override
    public void onToolbarLeftClickListener() {
        onRestoreViewStatus();
    }

    @Override
    protected void onBackPressed() {
        if (mSelectMode){
            onRestoreViewStatus();
            return;
        }
        super.onBackPressed();
    }

    protected void onCheckedListener(View view, int position,boolean isChecked){
        assert mAdapter.getData() != null;
        T bean = mAdapter.getData().get(position);
        if (isChecked){
            bean.setSelected(true);
            mSelectMap.put(position,mAdapter.getItem(position));
            String selectCount = String.format(getString(R.string.have_selected), requireActivity().getColor(R.color.color_5178FF),mSelectMap.size());
            mTvSelected.setText(Html.fromHtml(selectCount,Html.FROM_HTML_MODE_LEGACY));
            //全部选中，将全选按钮置为选中
            if (mSelectMap.size() == mAdapter.getData().size()){
                mRbSelectAll.setChecked(true);
            }
        }else {
            bean.setSelected(false);
            mSelectMap.remove(position);
            String selectCount = String.format(getString(R.string.have_selected), requireActivity().getColor(R.color.color_5178FF),mSelectMap.size());
            mTvSelected.setText(Html.fromHtml(selectCount,Html.FROM_HTML_MODE_LEGACY));
            //删除一个选中状态，全选按置为不选中
            if (mRbSelectAll.isChecked()){
                mRbSelectAll.setChecked(false);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mSelectMap != null && (!mSelectMap.isEmpty())){
            mSelectMap.clear();
        }
    }
}
